<template>
	<view>
		<map id="map" :longitude="longitude" :latitude="latitude" :style="contentStyle" scale="15" :enable-traffic="false" :show-location="true" :enable-poi="true" class="map">
			<cover-image class="location" src="../../static/workbench/location.png" @tap="returnLocationHandle()"></cover-image>
		</map>
		<view class="panel">
			<view class="from" @tap="chooseLocationHandle('from')">
				<text>{{ from.address }}</text>
			</view>
			<view class="dashed-line"></view>
			<view class="to" @tap="chooseLocationHandle('to')">
				<text>{{ to.address }}</text>
			</view>
		</view>
	</view>
</template>

<script>
const chooseLocation = requirePlugin('chooseLocation');
let QQMapWX = require('../../lib/qqmap-wx-jssdk.min.js');

export default {
	data() {
		return {
			from: {
				address: '输入你的出发地',
				longitude: 0,
				latitude: 0
			},
			to: {
				address: '输入你的目的地',
				longitude: 0,
				latitude: 0
			},
			longitude: 118.08934,
			latitude: 24.62714,
			contentStyle: '',
			windowHeight: 0,
			map: null,
			flag: null
		};
	},
	methods: {
		//回到定位位置函数
		returnLocationHandle: function() {
			let that = this;
		    this.map.moveToLocation({
				success(){
					that.map.getCenterLocation({
						success(res){
							that.latitude = res.latitude;
							that.longitude = res.longitude;
							wx.getSystemInfo({
								success: (res) => {
									console.log('SystemInfo:', res.platform);
									if(res.platform != 'android' && res.platform != 'ios') {
										//解决电脑端不更新定位演示问题
										let location = { latitude: 24.62714, longitude: 118.08934 };
										uni.$emit('updateLocation', location);
									}
								}
							});
						}
					});
				}
			});
		},
		chooseLocationHandle: function(flag) {
		    let key = this.tencent.map.key; //使用在腾讯位置服务申请的key
		    let referer = this.tencent.map.referer; //调用插件的app的名称
		    let latitude = this.latitude;
		    let longitude = this.longitude;
		    this.flag = flag;
		    let data = JSON.stringify({
		        latitude: latitude,
		        longitude: longitude
		    });
		    uni.navigateTo({
		        url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&location=${data}`
		    });
		}
	},
	onShow: function() {
		let that = this;
		that.map = uni.createMapContext('map');
		let qqmapsdk = new QQMapWX({
			key: that.tencent.map.key
		});
		
		//监听自定义事件:实时获取定位
		uni.$on('updateLocation', function(location) {
			console.log(location);
			//避免地图选点的内容被逆地址解析覆盖
			
			if(that.flag!=null){
				return
			}
			let latitude = location.latitude;
			let longitude = location.longitude;
			that.latitude = latitude;
			that.longitude = longitude;
			that.from.latitude = latitude;
			that.from.longitude = longitude;
			//把坐标解析成地址
			qqmapsdk.reverseGeocoder({
				location: {
					latitude: latitude,
					longitude: longitude
				},
				success: function(resp) {
					//console.log(resp);
					that.from.address = resp.result.address;
				},
				fail: function(error) {
					console.log(error);
				}
			});
		});
		
		//未获取到实时定位权限的临时解决方案:获取一次定位
		this.returnLocationHandle();
		
		//获取选点位置
		let location = chooseLocation.getLocation();
		if (location != null) {
			let place = location.name;
			let latitude = location.latitude;
			let longitude = location.longitude;
			if (that.flag == 'from') {
				that.from.address = place;
				that.from.latitude = latitude;
				that.from.longitude = longitude;
			} else {
				that.to.address = place;
				that.to.latitude = latitude;
				that.to.longitude = longitude;
				//跳转到创建订单,线路页面
				uni.setStorageSync("from",that.from)
				uni.setStorageSync("to",that.to)
				uni.navigateTo({
					url: `../create_order/create_order`
				});
			}
		}
	},
	onHide: function() {
		uni.$off('updateLocation');
		//清除地图选点的结果
		chooseLocation.setLocation(null);
	},
	onLoad: function() {
		let that = this;
		let windowHeight = uni.getSystemInfoSync().windowHeight;
		that.windowHeight = windowHeight;
		that.contentStyle = `height:${that.windowHeight}px;`;
		//查询乘客当前订单
		that.ajax(
			that.url.hasCustomerCurrentOrder,
			'POST',
			{},
			function(resp) {
				let result = resp.data.result;
				let hasCustomerUnAcceptOrder = result.hasCustomerUnAcceptOrder;
				let hasCustomerUnFinishedOrder = result.hasCustomerUnFinishedOrder;
				let hasCustomerUnPayedOrder = result.hasCustomerUnPayedOrder;
				if (hasCustomerUnAcceptOrder) {
					let json = result.unAcceptOrder;
					let carType = json.carType;
					let carPlate = json.carPlate;
					//获取订单过期时间,计算剩余秒数
					let createTime = new Date(json.createTime);
					let expireTime = 15*60 - (new Date() - createTime)/1000;
					let startPlaceLocation = JSON.parse(json.startPlaceLocation);
					let endPlaceLocation = JSON.parse(json.endPlaceLocation);
					let from = {
						address: json.startPlace,
						latitude: startPlaceLocation.latitude,
						longitude: startPlaceLocation.longitude
					};
					let to = {
						address: json.endPlace,
						latitude: endPlaceLocation.latitude,
						longitude: endPlaceLocation.longitude
					};
					uni.setStorageSync('from', from);
					uni.setStorageSync('to', to);
					uni.showModal({
						title: '提示消息',
						content: '您有一个订单等待司机接单，现在将跳转到等待接单页面',
						showCancel: false,
						success: function(resp) {
							if (resp.confirm) {
								uni.navigateTo({
									url: `../create_order/create_order?showPopup=true&orderId=${json.id}&showCar=true&carType=${carType}&carPlate=${carPlate}&expireTime=${expireTime}`
								});
							}
						}
					});
				} else if (hasCustomerUnFinishedOrder) {
					console.log(result.unFinishedOrder);
					uni.showModal({
						title: '提示消息',
						content: '您有一个正在执行的订单，现在将跳转到司乘同显画面',
						showCancel: false,
						success: function(resp) {
							if (resp.confirm) {
								uni.navigateTo({
									url: '../move/move?orderId=' + result.unFinishedOrder
								});
							}
						}
					});
				} else if(hasCustomerUnPayedOrder) {
					uni.showModal({
						title: '提示消息',
						content: '您有一个未支付的订单，现在将跳转到支付页面',
						showCancel: false,
						success: function(resp) {
							if (resp.confirm) {
								uni.navigateTo({
									url: '../order/order?orderId=' + result.unPayedOrder
								});
							}
						}
					});
				}
			},
			false
		);
	},
	onUnload: function() {
		//清除地图选点的结果
		chooseLocation.setLocation(null);
		this.flag = null;
	}
};
</script>

<style lang="less">
@import url('workbench.less');
</style>
